package com.example.satokentest.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.satokentest.common.response.Result;
import com.example.satokentest.model.dto.LoginDTO;
import com.example.satokentest.service.AuthService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {
    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody @Valid LoginDTO loginDTO) {
        return authService.login(loginDTO);
    }

    @PostMapping("/logout")
    public Result<?> logout() {
        return authService.logout();
    }

    @GetMapping("/info")
    public Result<?> getUserInfo() {
        return authService.getUserInfo();
    }

    @RequestMapping("/test")
    public Result<?> test(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return Result.success(StpUtil.getTokenValue());
        }
        return Result.error("登录失败");
    }
} 